package nostalgia.gui.forms;

import nostalgia.gui.IProgressRunnable;

import javax.swing.*;

public class ProgressDialog extends JDialog {
    private JPanel contentPane;
    private JLabel titleLabel;
    private JLabel statusLabel;

    protected ProgressDialog() {
        setContentPane(contentPane);
        setModal(true);
        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
    }

    protected ProgressCallback getProgressCallback() {
        return new ProgressCallback();
    }

    public static void showDialog(JFrame parent, String title, IProgressRunnable runnable) {
        ProgressDialog dialog = new ProgressDialog();
        dialog.titleLabel.setText(title);
        dialog.statusLabel.setText("XXX");
        dialog.pack();
        dialog.statusLabel.setText("");
        dialog.setLocationRelativeTo(parent);
        runnable.start(dialog.getProgressCallback());
        dialog.setVisible(true);
    }

    public class ProgressCallback implements IProgressRunnable.IProgressCallback {
        public void updateStatus(String text) {
            statusLabel.setText(text);
        }

        public void finished() {
            dispose();
        }

        public void error(String message) {
            JOptionPane.showMessageDialog(ProgressDialog.this, message);
        }
    }
}
